package Cinema.CinemaEntities;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import Utils.XmlParseHelper;
import Cinema.DataLayer.IXmlParsable;
import Cinema.DataLayer.XmlDefines;
import Exceptions.WrongXMLElement;


/**
 * Represents a movie in Cinema system.
 * @author The Fuchtzeinsoft Team.
 *
 */
public class Movie implements IXmlParsable {

	
	private String 	name;
	private String 	genre;

	/**
	 * creates a new Movie instance.
	 * @param name - the name of the movie.
	 * @param Genre - the Genre of the movie
	 */
	public Movie(String name, String Genre){
		this.name = name;
		this.genre = Genre;

	}


	/**
	 * Constructor for initializing 'Movie' from XML.
	 * @param element
	 * @throws WrongXMLElement
	 */
	public Movie(Element element) throws WrongXMLElement {
		initFromXml(element);
	}

	/**
	 * check if two object are equals. used for tests only.
	 */
	public boolean checkEquals(Object obj) {

		if (!(obj instanceof Movie)){
			return false;
		}

		Movie movie = (Movie) obj;

		return movie.name.equals(name) && movie.genre.equals(genre);
	}


	public String getName(){
		return name;
	}


	public String getGenre(){
		return genre;
	}

	@Override
	public void initFromXml(Element xmlElement) throws WrongXMLElement {

		//verifying that write xml element given
		if (!xmlElement.getTagName().equals(XmlDefines.MOVIE)){
			throw new WrongXMLElement();
		}

		//init name and type of the movie
		name = XmlParseHelper.getFirstChildByName(xmlElement, XmlDefines.NAME).getTextContent();
		genre = XmlParseHelper.getFirstChildByName(xmlElement, XmlDefines.GENRE).getTextContent();

	}

	@Override
	public Element getXml(Document document) {

		//create new xml element which will store the data
		Element element = document.createElement(XmlDefines.MOVIE);

		//create and append movie name element
		Element nameElement = document.createElement(XmlDefines.NAME);
		nameElement.setTextContent(name);
		element.appendChild(nameElement);

		//create and append movie type element
		Element typeElement = document.createElement(XmlDefines.GENRE);
		typeElement.setTextContent(genre);
		element.appendChild(typeElement);

		return element;
	}

	@Override
	public void initFromXml(boolean isRestorinf, Element xmlElement)
			throws WrongXMLElement {
		initFromXml(xmlElement);
	}

}
